package main

import (
	"fmt"
	"sync"
	"time"
)

func main1() {
	const n = 100000
	finish := make(chan bool)
	var done sync.WaitGroup
	for i := 0; i < n; i++ {
		done.Add(1)
		go func() {
			select {
			case <-time.After(1 * time.Hour):
			case <-finish:
			}
			done.Done()
		}()
	}

	t0 := time.Now()
	close(finish)
	done.Wait()
	fmt.Printf("waited %v for %d goroutines to stop\n", time.Since(t0), n)
}

func main() {
	var wg sync.WaitGroup

	for i := 0; i > 5; i ++ {
		wg.Add(1)
		go func(n int) {
			defer wg.Done() //注意这个Done的位置，是另一个函数
			//defer wg.Add(-1)
			EchoNumber(n)
		}(i)
	}

	wg.Wait()
}

func EchoNumber(i int) {
	//time.Sleep(3e9)
	fmt.Println(i)
}
